package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.entity.Role;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.IProjectService;
import cn.edu.seu.ssny.service.IRoleService;
import cn.edu.seu.ssny.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/web/user")
public class WebUserController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(WebUserController.class);

	@Autowired
	private IUserService userService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IProjectService projectService;

	private List<Project> getUserProjects(HttpSession session) {
		User currUser = (User) session.getAttribute("currUser");
		Enterprise enterprise = (Enterprise) session.getAttribute("enterprise");
		List<Project> projects;
		if(currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL) {
			projects = currUser.getProjects();
		}
		else {
			projects =  projectService.findAllByEnterprise(enterprise);
		}
		return projects;
	}

	private List<Role> getRoles(HttpSession session) {
		User currUser = (User) session.getAttribute("currUser");
		List<Role> roles;
		if(currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL) {
			roles = roleService.findAllByLevel(0);
		}
		else {
			roles =  roleService.findAll();
		}
		return roles;
	}

	@RequestMapping(value = { "/", "/index" })
	public String index(ModelMap map, HttpSession session) {
		map.put("projects", getUserProjects(session));
		return "web/user/index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map, HttpSession session) {
		// 向map中加入projects和roles
		map.put("roles", getRoles(session));
		return "web/user/form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Integer id, ModelMap map, HttpSession session) {
		// 向map中加入projects和roles
		map.put("roles", getRoles(session));
		User user = userService.find(id);
		map.put("user", user);
		return "web/user/form";
	}

	@RequestMapping(value = "/grant/{id}", method = RequestMethod.GET)
	public String grant(@PathVariable Integer id, ModelMap map, HttpSession session) {
		User user = userService.find(id);
		map.put("user", user);

		List<Project> set = user.getProjects();
		List<Integer> projectIds = new ArrayList<Integer>();
		for (Project project : set) {
			projectIds.add(project.getId());
		}
		map.put("projectIds", projectIds);

		map.put("projects", getUserProjects(session));

		return "web/user/grant";
	}
}
